package org.jeecg.modules.api.wlhy.setting;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.JsonObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.util.DictUtil;
import org.jeecg.modules.wlhy.hyarea.entity.HyArea;
import org.jeecg.modules.wlhy.hyarea.service.IHyAreaService;
import org.jeecg.modules.wlhy.hydriver.entity.HyDriver;
import org.jeecg.modules.wlhy.hydriver.service.IHyDriverService;
import org.jeecg.modules.wlhy.order.entity.HyOrder;
import org.jeecg.modules.wlhy.order.service.IHyOrderService;
import org.jeecg.modules.wlhy.setting.entity.HySetting;
import org.jeecg.modules.wlhy.setting.service.IHySettingService;
import org.jeecg.modules.wlhy.shipping.controller.HttpURLConnectionUtil;
import org.jeecg.modules.wlhy.utils.HyConfigUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Description: 动态设置接口
 * @author: luruirui
 * @date: 2021年06月04日 17:08
 */
@Api(tags="APP获取动态设置")
@RestController
@RequestMapping("/api/wlhy/setting")
@Slf4j
public class ApiSettingController extends JeecgController<HySetting, IHySettingService> {
    @Autowired
    private IHySettingService hySettingService;

    @Autowired
    private IHyOrderService orderService;
    @Autowired
    private IHyDriverService driverService;

    @Autowired
    private IHyAreaService hyAreaService;
    @Autowired
    private DictUtil dictUtil;

    //@Autowired
    //private JssReportService jssReportService;

    //获取设置列表
//    @AutoLog(value = "APP获取动态设置-获取设置列表")
    @ApiOperation(value="APP获取动态设置-获取设置列表", notes="APP获取动态设置-获取设置列表")
    @GetMapping(value = "/list")
    public Result<?> list() {
        return Result.OK(hySettingService.getList());
    }
    /**
     *  通过tag获取设置值
     *
     * @param tag
     * @return
     */
//    @AutoLog(value = "APP获取动态设置-通过tag获取设置值")
    @ApiOperation(value="APP获取动态设置-通过tag获取设置值", notes="APP获取动态设置-通过tag获取设置值")
    @GetMapping(value = "/getSettingValueByTag")
    public Result<?> getSettingValueByTag(@RequestParam(name="tag",required=true)String tag) {
        if (tag == null || StringUtils.isEmpty(tag)){
            return Result.error("请输入正确的tag！");
        }
        String settingValue = hySettingService.getSettingValueByTag2(tag);
        return Result.OK(settingValue);
    }

    /**
     *  获取司机APP登录页logo
     *
     * @return
     */
//    @AutoLog(value = "APP获取动态设置-获取司机APP登录页logo")
    @ApiOperation(value="APP获取动态设置-获取司机APP登录页logo", notes="APP获取动态设置-获取司机APP登录页logo")
    @GetMapping(value = "/driverLoginLogo")
    public Result<?> getDriverLoginLogo() {
        String driver_login_logo = hySettingService.getSettingValueByTag2("driver_login_logo");
        return Result.OK(driver_login_logo);
    }

    /**
     *  获取货主APP登录页logo
     *
     * @return
     */
//    @AutoLog(value = "APP获取动态设置-获取货主APP登录页logo")
    @ApiOperation(value="APP获取动态设置-获取货主APP登录页logo", notes="APP获取动态设置-获取货主APP登录页logo")
    @GetMapping(value = "/customerLoginLogo")
    public Result<?> getCustomerLoginLogo() {
        String customer_login_logo = hySettingService.getSettingValueByTag2("customer_login_logo");
        return Result.OK(customer_login_logo);
    }

    @ApiOperation(value="wx消息订阅获取appid和Secret", notes="wx消息订阅获取appid和Secret")
    @GetMapping(value = "/getAppidAndSecret")
    public Result<?> getAppidAndSecret() {
        String appid = HyConfigUtils.getConfig().getErweimaAppid();
        String secret = HyConfigUtils.getConfig().getErweimaSecret();
        String templateId = HyConfigUtils.getConfig().getTemplateId();
        JSONObject obj = new JSONObject();
        obj.put("appid", appid);
        obj.put("secret", secret);
        obj.put("templateId", templateId);
        if(StringUtils.isNotEmpty(appid)&&StringUtils.isNotEmpty(secret)&&StringUtils.isNotEmpty(templateId)){
            return Result.OK(obj);
        }else{
            return Result.error("");
        }

    }

    @GetMapping(value = "/ceshi")
    public Result<?> ceshi() {
        List<HyDriver> hyDriverList = driverService.list();
        HyOrder hyOrder = orderService.getOne(new QueryWrapper<HyOrder>().eq("order_num","HY202203251758582746"));
        HyArea sendAreaCode = getAreaNames(hyOrder.getSendAreaCode() + "000000");
        HyArea receiveAreaCode = getAreaNames(hyOrder.getReceiveAreaCode() + "000000");

        List<String> sendNames = Arrays.asList(sendAreaCode.getMergerName().split(","));
        List<String> receiveNames = Arrays.asList(receiveAreaCode.getMergerName().split(","));
        hyOrder.setSendAddressName(sendNames.get(1));
        hyOrder.setReceiveAddressName(receiveNames.get(1));
        if(hyOrder!=null){
            JSONObject objectDict = dictUtil.parseObjectDict(hyOrder);
            for (HyDriver driver : hyDriverList) {
                if(StringUtils.isNotEmpty(driver.getWxOpenid()) ){
                    String push = SendWxMessage.push(driver.getWxOpenid(), objectDict);
                    System.out.println(push);
                }

            }
        }
        return Result.OK();
    }

    private HyArea getAreaNames(String areaCode) {
        LambdaQueryWrapper<HyArea> queryWrapperArea = new LambdaQueryWrapper<>();
        queryWrapperArea.eq(HyArea::getAreaCode, areaCode);
        return hyAreaService.getOne(queryWrapperArea);
    }




//    @ApiOperation(value="APP获取动态设置-test用户获取设置列表", notes="APP获取动态设置-test用户获取设置列表")
//    @GetMapping(value = "/getAllUser")
//    public List<Usert> getDriverinfo() {
//        return jssReportService.getAllUsers();
//    }
//    @ApiOperation(value="APP获取动态设置-test添加用户", notes="APP获取动态设置-test添加用户")
//    @PostMapping(value = "/addUser")
//    public Usert addUser( @RequestBody Usert user ) {
//        return jssReportService.addUser( user );
//    }
//
//    @ApiOperation(value="APP获取动态设置-test删除用户", notes="APP获取动态设置-test删除用户")
//    @PostMapping(value = "/deleteUser")
//    public int deleteUser( @RequestBody Usert user ) {
//        return jssReportService.deleteUser( user );
//    }
}
